# -*- coding: utf-8 -*-
__author__ = 'Paul'

from plone.app.layout.viewlets.interfaces import IAboveContent
from plone.app.layout.viewlets.interfaces import IAboveContentBody

from plone.dexterity.interfaces import IDexterityContent
from plone.app.layout.globals.interfaces import IViewView
from Products.bpTheme.interfaces import IBpTheme
from zope.interface import Interface
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
from plone.app.layout.globals.interfaces import IViewView
from Products.ATContentTypes.interface import IATFolder
from zope.site.hooks import getSite
from zope.component import getMultiAdapter
from Products.bpContents.libro import ILibro
from Products.bpContents.articulo import IArticulo
from Products.bpContents.galeria import IGaleria
from plone.memoize.instance import memoize
from  plone.app.layout.viewlets.interfaces import IHtmlHead

from Acquisition import aq_inner

from plone.app.layout.viewlets.interfaces import IPortalHeader

# Import default Plone viewlet classes
from plone.app.layout.viewlets              import common as base

class MetadataViewlet(base.ViewletBase):


    @memoize
    def chkViable(self):
        """Verifica que esté en un articulo, galeria, libro, o evento,"""

        if self.context.portal_type in ("Products.bpContents.articulo","Products.bpContents.galeria","Products.bpContents.evento","Products.bpContents.libro",):
            return True
        else:
            return False


    def dameDatos(self):
        datos=dict()
        contexto=self.contexto

        datos["titulo"]= contexto.Title()
        datos["descri"]=contexto.Description()
        datos["url"]=contexto.absolute_url()
        datos["autor"]=contexto.Creator()
        datos["modificado"]=contexto.ModificationDate()
        if contexto.portal_type in ("Products.bpContents.evento","Event"):
            datos["tipoContenido"]='event'
            datos["evento"]=self.contexto.start.isoformat()

        elif contexto.portal_type == "Image":
            datos["tipoContenido"]='image'
        else:
            datos["tipoContenido"]='article'

        datos["img"]='http://centos:8080/++theme++Products.bpTheme/img/logo_bp.gif'
        if contexto.portal_type in ("Products.bpContents.articulo","Products.bpContents.evento","Products.bpContent.galeria",):
            imgs=self.getImages(contexto)
            if imgs and len(imgs)>0:
                datos["img"]="%s/image_thumb" %imgs[0]['url']



        return datos

    def update(self):
        pass__author__ = 'Paul'
        self.contexto=aq_inner(self.context)

    def getImages(self,obj):
        """Toda las imagenes
        """
        from Products.bpContents.getImgTira import GetTiraImgs
        context = GetTiraImgs(obj)
        results = context.getTira()
        if results:
            return results
        else:
            return None
